package com.sixiaolong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixiaolong.dao.SetmealDao;
import com.sixiaolong.domain.SetMealDto;
import com.sixiaolong.domain.Setmeal;
import com.sixiaolong.domain.SetmealDish;
import com.sixiaolong.service.SetMealDishService;
import com.sixiaolong.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private SetMealDishService setMealDishService;

    //   根据id获取 套餐信息
    @Override
    public SetMealDto getByIdSetMeal(Long id) {
        Setmeal setmeal = this.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setmeal,setMealDto);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setMealDishService.list(lqw);
        setMealDto.setSetmealDishes(setmealDishes);
        return setMealDto;
    }

//    添加新套餐
    @Override
    @Transactional
    public void saveSetMeal(SetMealDto setMealDto) {
//        保存到setmeal里
        this.save(setMealDto);
//        缺少setmealid  需要加上
        List<SetmealDish> list = new ArrayList<>();
        setMealDto.getSetmealDishes().forEach(e -> {
//            SetmealDish setmealDish = new SetmealDish();
//            BeanUtils.copyProperties(e,setmealDish);
//            setmealDish.setSetmealId(setmealDish.getId());
            e.setSetmealId(setMealDto.getId());
            list.add(e);
        });
        setMealDishService.saveBatch(list);
    }
}
